# Copyright (C) 2006  Apptility LLC  http://www.apptility.com
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA


class AjubySugarcrmApi

  require 'integration/sugarcrm/ajuby_sugarcrm_pipe'
  
  API_STRING_CONST = "AjubySugarcrmAPI"
  
  def initialize session
    @session = session
    
    @sugarcrmpipe = AjubySugarcrmPipe.new()
    session['sugarcrm_login_context'] = @sugarcrmpipe.login_context
    AjubyLogger.logInfo "sugarcrmpipe: #{session['sugarcrm_login_context']}"
  end


  def test_connection
    sugarcrmpipe.login
  end
      
  def get_modules
    begin
      return sugarcrmpipe.get_modules_list
    rescue Exception => e
      raise AjubySugarcrmPipe.build_err_trace("#{API_STRING_CONST}: get_modules call failed : " << e.message)
    end  
  end

  def get_module_records(module_name)
    begin
      sugarcrmpipe.get_module_records(100,module_name)
    rescue Exception => e
      raise AjubySugarcrmPipe.build_err_trace("#{API_STRING_CONST}: get_module_records call failed : " << e.message)
    end  

  end
  
  def get_user_records(module_name,id)
    begin
      sugarcrmpipe.get_user_records(100,module_name,id)
    rescue Exception => e
      raise AjubySugarcrmPipe.build_err_trace("#{API_STRING_CONST}: get_user_records call failed : " << e.message)
    end  

  end
 
  def create_new_contact(first_name,last_name,email_address)
    begin
      sugarcrmpipe.create_new_contact(first_name,last_name,email_address)
    rescue Exception => e
      raise AjubySugarcrmPipe.build_err_trace("#{API_STRING_CONST}: create_new_contact call failed : " << e.message)
    end  
  end
  
  def create_new_lead(first_name,last_name,email_address)
    begin
      sugarcrmpipe.create_new_lead(first_name,last_name,email_address)
    rescue Exception => e
      raise AjubySugarcrmPipe.build_err_trace("#{API_STRING_CONST}: create_new_lead call failed : " << e.message)
    end  
  end
  
  def create_new_account(name,phone,website)
    begin
      sugarcrmpipe.create_new_account(name,phone,website)
    rescue Exception => e
      raise AjubySugarcrmPipe.build_err_trace("#{API_STRING_CONST}: create_new_account call failed : " << e.message)
    end  
  end
  
  def create_new_opportunity(name,amount)
    begin
      sugarcrmpipe.create_new_opportunity(name,amount)
    rescue Exception => e
      raise AjubySugarcrmPipe.build_err_trace("#{API_STRING_CONST}: create_new_opportunity call failed : " << e.message)
    end  
  end
  
  def create_new_case(name)
    begin
      sugarcrmpipe.create_new_case(name)
    rescue Exception => e
      raise AjubySugarcrmPipe.build_err_trace("#{API_STRING_CONST}: create_new_case call failed : " << e.message)
    end  
  end
  
  def create_new_project(name)
    begin
      sugarcrmpipe.create_new_project(name)
    rescue Exception => e
      raise AjubySugarcrmPipe.build_err_trace("#{API_STRING_CONST}: create_new_project call failed : " << e.message)
    end  
  end
  
   def create_new_projecttask(name,project_id)
    begin
      sugarcrmpipe.create_new_projecttask(name,project_id)
    rescue Exception => e
      raise AjubySugarcrmPipe.build_err_trace("#{API_STRING_CONST}: create_new_projecttask call failed : " << e.message)
    end  
  end
  
  def sugarcrm_login_context
    session['sugarcrm_login_context']
  end

  def sugarcrmpipe
    @sugarcrmpipe
  end
  
  def current_session
    @session
  end
  
end